package com.angel.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

/*网络通信接口工具类，发送获取请求*/
public class HttpUtil {
	public static HttpClient httpClient = new DefaultHttpClient();
	// public static String baseUrl="http://127.0.1.2:9090/";
	// public static String baseUrl="http://127.0.0.1/:9090/";//usb线下电脑不断网下地址
	// public static String
	// baseUrl="http://ljtcherish123.xicp.net:9090/MorningAngel/android/";//wifi下服务器端地址
	// public static String picUrl="http://ljtcherish123.xicp.net:9090/";
	public static String baseUrl = "http://192.168.0.112:9090/MorningAngel/android/";// wifi下服务器端地址
	public static String picUrl = "http://192.168.0.112:9090/MorningAngel/";

	public static String IP = "183.3.166.108";
	public static int TIME_OUT_DELAY = 20000;// 设置连接超时，请求超时时间

	/* httpGet请求，获取请求内容 */
	public static String httpGet(String url) {
		initHttp();
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = httpClient.execute(httpGet);

			if (200 == response.getStatusLine().getStatusCode()) {
				String result = EntityUtils.toString(response.getEntity(),
						"utf-8");
				return result;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return "{'isSuccess':'2'}";
		} catch (IOException e) {
			e.printStackTrace();
			return "{'isSuccess':'2'}";
		}
		return null;
	}

	/* httpPost请求，获取请求内容 */
	public static String httpPost(String url, Map<String, String> params)
			throws UnsupportedEncodingException {
		initHttp();
		HttpPost httpPost = new HttpPost(url);
		List<NameValuePair> valuePairs = new ArrayList<NameValuePair>();

		for (String key : params.keySet()) {// 将请求内容封装到NameValuePair中
			valuePairs.add(new BasicNameValuePair(key, params.get(key)));
		}
		httpPost.setEntity(new UrlEncodedFormEntity(valuePairs, "utf-8"));
		try {
			HttpResponse httpResponse = httpClient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String result = EntityUtils.toString(httpResponse.getEntity(),
						"utf-8");
				return result;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return "{'isSuccess':'2'}";
		} catch (IOException e) {
			e.printStackTrace();
			return "{'isSuccess':'2'}";
		}
		return null;
	}

	/**
	 * 分别设置连接超时，请求超时
	 */
	public static void initHttp() {
		httpClient.getParams().setIntParameter(
				HttpConnectionParams.CONNECTION_TIMEOUT, TIME_OUT_DELAY);
		httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
				TIME_OUT_DELAY);
	}
}